package com.useeinfo.oa.modules.wuHanCommon.base.biz;

import com.useeinfo.framework.extend.biz.CrudBiz;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.func.excel.ExcelImport_New;
import com.useeinfo.oa.common.utils.ResultUtil.ResultDto;
import com.useeinfo.oa.common.utils.ResultUtil.ResultUtil;
import com.useeinfo.oa.modules.wuHanCommon.base.dao.WuHanPointDao;
import com.useeinfo.oa.modules.wuHanCommon.base.entity.WuHanPoint;
import com.useeinfo.oa.modules.wuHanCommon.base.entity.WuHanUnit;
import com.useeinfo.oa.modules.wuHanCommon.base.enums.PointSystem;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.hibernate.service.spi.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Service
public class WuHanPointBiz extends CrudBiz<WuHanPointDao, WuHanPoint> {

    @Autowired
    private WuHanUnitBiz wuHanUnitBiz;

    /**
     * 根据名称搜索点位
     *
     * @param name 点位名称
     * @return
     */
    public WuHanPoint getWuHanPointByName(String name) {
        if (StringUtils.isEmpty(name)) {
            return null;
        }
        QueryParam queryParam = new QueryParam();
        queryParam.getSqlMap().put("name", name);
        List<WuHanPoint> wuHanPointList = this.findList(queryParam);
        if (CollectionUtils.isNotEmpty(wuHanPointList)) {
            return wuHanPointList.get(0);
        }
        return null;
    }

    /**
     * 查询是否存在
     *
     * @param wuHanPoint
     * @return
     */
    public Boolean existWuHanPoint(WuHanPoint wuHanPoint) {
        if (StringUtils.isBlank(wuHanPoint.getName())) {
            return true;
        }
        QueryParam queryParam = new QueryParam();
        queryParam.getSqlMap().put("name", wuHanPoint.getName());
        List<WuHanPoint> wuHanPoints = findList(queryParam);

        if (wuHanPoint.getId() == null) {
            return CollectionUtils.isNotEmpty(wuHanPoints);
        } else {
            return !((wuHanPoints.size() == 1 && wuHanPoints.get(0).getId().equals(wuHanPoint.getId())) || wuHanPoints.size() == 0);
        }
    }


    /**
     * 导入点位数据
     *
     * @param file 导入的用户数据文件
     */
    @Transactional(readOnly = false)
    public ResultDto importData(MultipartFile file) {
        if (file == null) {
            throw new ServiceException("请选择导入的数据文件！");
        }
        int failureNum = 0;
        StringBuilder failureMsg = new StringBuilder();
        try (ExcelImport_New ei = new ExcelImport_New(file, 1, 0)) {
            List<WuHanPoint> list = ei.getDataList(WuHanPoint.class);
            for (WuHanPoint point : list) {
                if (existWuHanPoint(point)) {
                    failureNum++;
                    failureMsg.append("<br/>").append(failureNum).append("、点位 ").append(point.getName()).append(" 已存在");
                    continue;
                }
                if (StringUtils.isNotBlank(point.getUnit())) {
                    WuHanUnit wuHanUnit = wuHanUnitBiz.getWuHanUnit(point.getUnit());
                    if (wuHanUnit == null) {
                        failureNum++;
                        failureMsg.append("<br/>").append(failureNum).append("、设备 ").append(point.getUnit()).append(" 不存在");
                        continue;
                    }
                    point.setWuHanUnit(wuHanUnit);
                }
                addOrUpdate(point);
            }
        } catch (Exception e) {
            failureMsg.append(e.getMessage());
            logger.error(e.getMessage(), e);
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        return ResultUtil.success();
    }
}
